import request from '@/http';
import uploadHttp from '@/http/uploadHttp';
import { Problem, ProblemListResponse } from '@/types/problem';

export interface TestCaseInfo {
  input_name: string;
  output_name: string;
  input_size: number;
  output_size: number;
  stripped_output_md5: string;
}

export interface TestCaseResponse {
  id: string;
  info: TestCaseInfo[];
  spj: boolean;
}

export interface ImportResponse {
  code: number;
  message: string;
  data: {
    import_count: number;
  } | null;
  timestamp: string;
}

export const problemService = {
  getProblems(params: { page?: number; limit?: number; keyword?: string; contest_id?: number; rule_type?: string }) {
    return request.get<ProblemListResponse>('/api/problem/', { params });
  },

  getProblem(id: number, params?: { contest_id?: number }) {
    return request.get<Problem>(`/api/problem/${id}/`, { 
      params: params 
    });
  },

  getProgrammingProblem(id: string) {
    return request.get(`/api/problem/${id}/`);
  },

  createProblem(data: Partial<Problem>) {
    return request.post<Problem>('/api/problem/', data);
  },

  updateProblem(id: number, data: Partial<Problem>) {
    return request.put<Problem>(`/api/problem/${id}/`, data);
  },

  patchProblem(id: number, data: Partial<Problem>) {
    return request.patch<Problem>(`/api/problem/${id}/`, data);
  },

  deleteProblem(id: number) {
    return request.delete(`/api/problem/${id}/`);
  },

  uploadTestCase(formData: FormData) {
    return request.post<TestCaseResponse>('/api/testcase/', formData, {
      headers: {
        'Content-Type': 'multipart/form-data',
      },
    });
  },

  // 导入qduoj题目
  importQduojProblems(formData: FormData) {
    return uploadHttp.upload<ImportResponse>('/api/problem/import/', formData);
  },

  // 导入fps题目
  importFpsProblems(formData: FormData) {
    return uploadHttp.upload<ImportResponse>('/api/problem/import_fps/', formData);
  },

  // 下载测试样例
  downloadTestCase(problemId: number) {
    return request.get(`/api/testcase/`, {
      params: { problem_id: problemId },
      responseType: 'blob', // 设置响应类型为blob以处理文件下载
    });
  },

  // 将普通题目转换为比赛题目
  convertToContestProblem(contestId: number, problemId: number) {
    return request.post(`/api/problem/convert_to_contest_problem/`, {
      contest_id: contestId,
      _id: problemId
    });
  },
}; 